/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.fd.client;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.xy.biz.fd.domain.req.folder.CreateManufactureFolderRequest;
import com.xy.biz.fd.domain.req.folder.SaveFolderRequest;
import com.xy.biz.fd.domain.resp.GetDocumentResponse;
import com.xy.biz.fd.domain.resp.GetFolderResponse;
import com.xy.biz.fd.domain.req.folder.GetFolderRequest;
import com.xy.biz.fd.domain.req.folder.ListFolderRequest;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.util.ResultUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;

/**
 *
 *
 * @author ssl
 * @date 2021-1-23
 */
public interface FolderClient {

    /**
     * 保存文件夹
     * @param request
     * @return
     */
    public Result<GetFolderResponse> saveFolder(SaveFolderRequest request);

    /**
     * 查找一个
     *
     * @param request
     * @return
     */
    Result<GetFolderResponse> getFolder(GetFolderRequest request);

    /**
     * 查找多个
     *
     * @param collection 标识集合
     * @return
     */
    Result<List<GetFolderResponse>> listFolderById(Collection<Long> collection);

    /**
     * 查找多个
     *
     * @param request 查找条件
     * @return
     */
    Result<?> listFolderDocument(ListFolderRequest request);

    /**
     * 树状图
     *
     * @param request 查找条件
     * @return
     */
    Result<?> listTreeFolderDocument(ListFolderRequest request);

    /**
     * 统计个数
     *
     * @param request 查找条件
     * @return
     */
    Result<Integer> countFolder(ListFolderRequest request);

    /**
     * 分页查找
     *
     * @param request 查找条件
     * @return
     */
    Result<Page<GetFolderResponse>> pageFolder(ListFolderRequest request);


    /**
     * 恢复文件夹
     *
     * @param request
     * @return
     */
    Result<?> recoverFolder(GetFolderRequest request);

    /**
     * 批量恢复文件夹
     *
     * @param folderIds
     * @return
     */
    Result<?> recoverFolderByIds(Collection<Long> folderIds);

    /**
     * 批量删除文件夹
     *
     * @param folderIds
     * @return
     */
    Result<?> deleteFolderByIds(Collection<Long> folderIds);


    /**
     * 获取发行文件当目录
     * @param request
     * @return
     */
    Result<List<GetFolderResponse>> getRelease(ListFolderRequest request);


    default <T> void buildByFolder(Collection<T> source,
                                   Collection<Long> folderIds,
                                   Function<T, Long> folderIdGetter,
                                   BiConsumer<T, GetFolderResponse> builder
    ) {
        Objects.requireNonNull(folderIdGetter, "The userIdGetter is required.");
        Objects.requireNonNull(builder, "The builder is required.");
        if (CollectionUtils.isEmpty(source)) {
            return;
        }
        if (CollectionUtils.isEmpty(folderIds)) {
            return;
        }
        Set<Long> userIdSet = new HashSet<>(folderIds);
        Result<List<GetFolderResponse>> listResult = listFolderById(userIdSet);
        List<GetFolderResponse> getFolderResponses = ResultUtils.get(listResult);
        if (!CollectionUtils.isEmpty(getFolderResponses)) {
            Map<Long, GetFolderResponse> folderResponseMap =
                    getFolderResponses.stream().collect(Collectors.toMap(resp -> resp.getFolderId(), resp -> resp));
            for (T t : source) {
                Long folderId = folderIdGetter.apply(t);
                GetFolderResponse getFolderResponse = folderResponseMap.get(folderId);
                if (!Objects.isNull(getFolderResponse)) {
                    builder.accept(t, getFolderResponse);
                }
            }
        }
    }

    Result<GetFolderResponse> createManufactureFolder(CreateManufactureFolderRequest request);
}
